﻿using Autofac;
using Autofac.Integration.Mvc;
using Pay.Core;
using Pay.Service;
using System.Linq;
using System.Reflection;
using System.Web.Mvc;

namespace Pay.H5 {
    /// <summary>
    /// 依赖注入[DI]
    /// </summary>
    public class DependencyInjectionConfig {
        /// <summary>
        ///注册依赖注入
        /// </summary>
        public static void Register() {
            var builder = new ContainerBuilder();
            //注册mvc容器的实现
            builder.RegisterControllers(Assembly.GetExecutingAssembly());
            builder.RegisterType<Repository>().As<IUnitOfWork>().InstancePerLifetimeScope();
            builder.RegisterAssemblyTypes(Assembly.Load("Pay.Service")).Where(c => c.IsSubclassOf(typeof(ServiceBase))).AsImplementedInterfaces();

            //注册日志
            string[] log_names = new string[] { "data_change", "sql_info", "data_error", "ui_error", "info" };
            foreach (var log_name in log_names)
                builder.Register<ILogHelper>(c => new LogHelper(log_name)).Named<ILogHelper>(log_name).SingleInstance();

            var container = builder.Build();
            IocHelper.InitializeWith(container);
            //注册MVC容器
            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
        }
    }
}